$(function () {
    /**
     * 初始化表单验证
     * 
     * @param {type} param
     */
    $("#changePwdForm").validate({
        //错误类名和元素标签类型
        errorClass: "gg-error",
        validClass: "gg-success",
        errorElement: "label",
        //验证规则正则表达式
        rules: {
            oldPwd: {
                required: true
            },
            newPwd: {
                required: true
            },
            confirmPwd: {
                required: true,
                equalTo: "#newPwd"
            }
        },
        //验证结果文字提示
        messages: {
            oldPwd: {
                required: '必填'
            },
            newPwd: {
                required: '必填'
            },
            confirmPwd: {
                required: '必填',
                equalTo: '两次输入的密码不一致'
            }
        },
        //验证结果处理
        highlight: function (element, errorClass, validClass) {
            $(element).closest('.form-group').addClass(errorClass).removeClass(validClass);
        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).closest('.form-group').removeClass(errorClass).addClass(validClass);
        },
        //错误信息位置
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        }
    });

    /**
     * 提交表单
     * 
     * @param {type} param
     */
    $("#changePwdBtn").click(function () {
        if ($("#changePwdForm").valid()) {
            var data = $("#changePwdForm").formToJson();
            $.post("employee/changePassword", data, function (res) {
                if (res.success) {
                    alert("密码修改成功");
                    location.href = "login";
                } else {
                    alert("密码修改失败");
                }
            }).fail(function () {
                alert("网络通信错误");
            });
        }
    });
});